using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CodeWork.DAL.BDHomePage;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_LinkCategoryEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        LinkCategory linkCategory;

        if (Int32.TryParse(Request["id"], out id))
            linkCategory = LinkCategory.Load(id);
        else
            linkCategory = null;

        if (linkCategory != null)
        {
            if (!string.IsNullOrEmpty(linkCategory.CategoryName))
				tbxCategoryName.Text = linkCategory.CategoryName;
			if (!string.IsNullOrEmpty(linkCategory.CategoryDescription))
				tbxCategoryDescription.Text = linkCategory.CategoryDescription;
			if (!string.IsNullOrEmpty(linkCategory.CategoryImageUrl))
				tbxCategoryImageUrl.Text = linkCategory.CategoryImageUrl;
			if (linkCategory.CreateDate != SqlDateTime.MinValue)
				tbxCreateDate.Text = linkCategory.CreateDate.ToShortDateString();
			tbxLinkCount.Text = linkCategory.LinkCount.ToString();

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string categoryName = Security.BindStringInput(tbxCategoryName.Text, _securityLevel);
		string categoryDescription = Security.BindStringInput(tbxCategoryDescription.Text, _securityLevel);
		string categoryImageUrl = Security.BindStringInput(tbxCategoryImageUrl.Text, _securityLevel);
		DateTime createDate = Security.BindDateTimeInput(tbxCreateDate.Text, _securityLevel);
		int linkCount = Security.BindIntegerInput(tbxLinkCount.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (LinkCategory.Save(
				id, 
				categoryName,
				categoryDescription,
				categoryImageUrl,
				createDate,
				linkCount
                ))
                    Response.Redirect("linkcategorylist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            LinkCategory linkCategory = LinkCategory.Create(
				categoryName,
				categoryDescription,
				categoryImageUrl,
				createDate,
				linkCount
                );

            if (linkCategory != null)
                Response.Redirect("linkcategorylist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("linkcategorylist.aspx");
    }
}
